var app = getApp(), fetchApi = require("../../../utils/httpPost.js")

import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
const message = '是否确认取消该订单？'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    pageSize:15,
    pageIndex: 1,
    scrollTop: 0,
    scrollHeight:0,
    hasData:false,
    orderList:[],
    loading: false,
    netWork: false,
    isLoading: true,
    visible:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  reload:function(){
    this.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/product/order/ag/list', {
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          console.log("订单列表数据", res)
          var orders = res.data.data.orders;
          if(orders.length > 0){
            for (var index in orders) {
              that.data.orderList.splice(that.data.orderList.length, 0, orders[index]);
            }
            that.setData({
              hasData: true,
              orderList: that.data.orderList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      this.data.pageIndex = 1
      this.initOp()
    }
  },
  goOrderDetail:function(t){
    console.log('t',t)
    var state = t.currentTarget.dataset.state
    var orderid = t.currentTarget.dataset.orderid
    wx.navigateTo({
      url: "/pages/order/detail/index?state=" + state + "&order_id=" + orderid
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.initOp()
  },
  initOp:function(){
    this.setData({
      currentTab: 0,
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      orderList:[]
    })
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.data.pageIndex = this.data.pageIndex + 1
    this.onHttpInit()
  },
})